package com.gewu.ordlelist.adapter;

import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.gewu.ordlelist.R;
import com.gewu.ordlelist.database.dbbean.ByCar;
import com.gewu.ordlelist.database.dbdao.ByCarDao;
import com.gewu.ordlelist.utils.actionUpdate;
import com.gewu.ordlelist.utils.jocUtils;

import java.util.List;
import java.util.Random;

public class thrAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    List<ByCar> quer;
    private com.gewu.ordlelist.utils.actionUpdate actionUpdate;

    public thrAdapter(List<ByCar> quer, com.gewu.ordlelist.utils.actionUpdate actionUpdate) {
        this.quer = quer;
        this.actionUpdate = actionUpdate;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.thr_item, parent, false);
        return new ThrItem(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        RoundedCorners roundedCorners = new RoundedCorners(50);
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
        int i = new Random().nextInt(6);
        Glide.with(holder.itemView).load(jocUtils.getimgs()[i]).apply(options).into(((ThrItem) holder).image);
        ((ThrItem) holder).text.setText(quer.get(position).getName() + "\n价格：" + quer.get(position).getMoney());
        ((ThrItem) holder).etAmount.setText(quer.get(position).getNum() +1+ "");
        ((ThrItem) holder).btnDecrease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ByCarDao byCarDao = new ByCarDao(holder.itemView.getContext());
                String s = ((ThrItem) holder).etAmount.getText().toString();
                int i1 = Integer.parseInt(s);
                if (i1 > 1) {
                    ((ThrItem) holder).etAmount.setText(i1 - 1 + "");
                    ByCar byCar = quer.get(position);
                    byCar.setNum(Integer.parseInt(((ThrItem) holder).etAmount.getText().toString())+1);
                    byCarDao.update(byCar);
                    actionUpdate.shuaxin(quer.size());
                }else if (i1==1){
                    jocUtils.showDilog(holder.itemView.getContext(),"提示！","您确定删除此菜品吗？")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            byCarDao.delectFoId(quer.get(position));
                            dialog.dismiss();
                            actionUpdate.shuaxin(quer.size());
                        }
                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(holder.itemView.getContext(), "请你继续添加此菜品。", Toast.LENGTH_SHORT).show();
                            dialog.dismiss();
                        }
                    }).show();
                }
            }
        });
        ((ThrItem) holder).btnIncrease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = ((ThrItem) holder).etAmount.getText().toString();
                int i1 = Integer.parseInt(s);
                if (i1 < 50) {
                    ((ThrItem) holder).etAmount.setText(i1 + 1 + "");
                    ByCar byCar = quer.get(position);
                    ByCarDao byCarDao = new ByCarDao(holder.itemView.getContext());
                    byCarDao.update(byCar);
                    byCar.setNum(Integer.parseInt(((ThrItem) holder).etAmount.getText().toString())+1);
                    actionUpdate.shuaxin(quer.size());
                }else {
                    jocUtils.showDilog(holder.itemView.getContext(),"提示！","您已经添加到最大数量!").show();
                }
            }
        });
        actionUpdate.shuaxin(quer.size());
    }

    @Override
    public int getItemCount() {
        return quer == null ? 0 : quer.size();
    }

    private class ThrItem extends RecyclerView.ViewHolder {

        private ImageView image;
        private TextView text;
        private Button btnDecrease;
        private TextView etAmount;
        private Button btnIncrease;

        public ThrItem(View inflate) {
            super(inflate);
            image = inflate.findViewById(R.id.image);
            text = inflate.findViewById(R.id.text);
            btnDecrease = inflate.findViewById(R.id.btnDecrease);
            etAmount = inflate.findViewById(R.id.etAmount);
            btnIncrease = inflate.findViewById(R.id.btnIncrease);
        }
    }
}
